home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / fly8111-.000 / fly8111- / fly8 / command.c < prev    next >
C/C++ Source or Header  |  1979-12-31  |  12KB  |  634 lines

  1. /* --------------------------------- command.c ------------------------------ */
  2.  
  3. /* This is part of the flight simulator 'fly8'.
  4.  * Author: Eyal Lebedinsky (eyal@ise.canberra.edu.au).
  5. */
  6.  
  7. /* process user commands.
  8. */
  9.  
  10. #include "plane.h"
  11.  
  12.  
  13. #define TO_AUTOCONNECT    5000        /* 5 secs to auto connect */
  14.  
  15. extern Ushort FAR
  16. set_lists (Ushort list)
  17. {
  18.     Ushort    old;
  19.  
  20.     old = st.flags & SF_LISTS;
  21.     st.flags &= ~SF_LISTS;
  22.     if (list && list != old)
  23.         st.flags |= list;
  24.     return (old);
  25. }
  26.  
  27. LOCAL_FUNC void NEAR
  28. swap_view (short old, short new)
  29. {
  30.     int    i;
  31.  
  32.     for (i = 0; i < NHDD; ++i) {
  33.         if ((st.hdd[i].flags & HDF_ON) && st.hdd[i].type == old) {
  34.             st.hdd[i].type = new;
  35.             break;
  36.         }
  37.     }
  38. }
  39.  
  40. LOCAL_FUNC void NEAR
  41. update_viewer (void)
  42. {
  43.     OBJECT *p;
  44.  
  45.     if (T(p = get_viewer ((st.flags1 & SF_EXTVIEW) ? st.extview : 0)))
  46.         save_viewport (p);
  47.     if (!(st.flags1 & SF_EXTVIEW))
  48.         save_viewport (CV);
  49. }
  50.  
  51. LOCAL_FUNC int NEAR
  52. one_command (int ch)
  53. {
  54.     int        i, j;
  55.     char        msg[80];
  56.     char        m[80];
  57.     int        cv;
  58.     OBJECT        *p;
  59.     E_PLANE        *ecv;
  60.     VIEWPORT    *vp;
  61.     int        ret;
  62.     Ushort        temp;
  63.  
  64.     ecv = IS_PLANE(CV) ? EE(CV) : 0;
  65.  
  66.     ret = 0;
  67.     switch (ch) {
  68.  
  69. #define WSTEP    FONE/64        /* ++++++++++++ start debug keys ++++++++++ */
  70.  
  71. /* arrows */    case KF_XRIGHT:                    /* move */
  72.         case KF_XLEFT:
  73.         case KF_XUP:
  74.         case KF_XDOWN:
  75.             if (CP->zoom > 0) {
  76.                 i = 1 << (CP->zoom / 3);
  77.                 j = CP->zoom % 3;
  78.                 if (1 == j)
  79.                     i += fmul (i, FCON(0.26));
  80.                 else if (2 == j)
  81.                     i += fmul (i, FCON(0.59));
  82.             } else
  83.                 i = 1;
  84.             i = D90/(18*i);
  85.             if (KF_XRIGHT == ch)
  86.                 CP->rotz += i;
  87.             else if (KF_XLEFT == ch)
  88.                 CP->rotz -= i;
  89.             else if (KF_XUP == ch)
  90.                 CP->rotx += i;
  91.             else
  92.                 CP->rotx -= i;
  93.             if (iabs (CP->rotz) < (Uint)i/2)
  94.                 CP->rotz = 0;
  95.             if (iabs (CP->rotx) < (Uint)i/2)
  96.                 CP->rotx = 0;
  97.             break;
  98. #if 0
  99.         case KF_RIGHT:
  100.             if (CW->orgx + CW->maxx < FONE-WSTEP) {
  101.                 show_fixed (1);
  102.                 CW->orgx += WSTEP;
  103.                 show_fixed (0);
  104.             }
  105.             break;
  106.         case KF_XLEFT:
  107.             if (CW->orgx - CW->maxx > WSTEP) {
  108.                 show_fixed (1);
  109.                 CW->orgx -= WSTEP;
  110.                 show_fixed (0);
  111.             }
  112.             break;
  113.         case KF_XUP:
  114.             if (CW->orgy - CW->maxy > WSTEP) {
  115.                 show_fixed (1);
  116.                 CW->orgy -= WSTEP;
  117.                 show_fixed (0);
  118.             }
  119.             break;
  120.         case KF_XDOWN:
  121.             if (CW->orgy + CW->maxy < FONE-WSTEP) {
  122.                 show_fixed (1);
  123.                 CW->orgy += WSTEP;
  124.                 show_fixed (0);
  125.             }
  126.             break;
  127. #endif
  128.  
  129. /* AltArrows */    case KF_YRIGHT:                    /* resize x */
  130.             show_fixed (1);
  131.             CW->maxx += WSTEP;
  132.             if (CW->orgx + CW->maxx > FONE)
  133.                 CW->maxx = FONE - CW->orgx;
  134.             if (CW->orgx - CW->maxx < 0)
  135.                 CW->maxx = CW->orgx;
  136.             show_fixed (0);
  137.             break;
  138.         case KF_YLEFT:
  139.             show_fixed (1);
  140.             CW->maxx -= WSTEP;
  141.             if (CW->maxx < FONE/128)
  142.                 CW->maxx = FONE/128;
  143.             show_fixed (0);
  144.             break;
  145.  
  146.         case KF_YUP:                    /* resize y */
  147.             show_fixed (1);
  148.             CW->maxy += WSTEP;
  149.             if (CW->orgy + CW->maxy > FONE)
  150.                 CW->maxy = FONE - CW->orgy;
  151.             if (CW->orgy - CW->maxy < 0)
  152.                 CW->maxy = CW->orgy;
  153.             show_fixed (0);
  154.             break;
  155.         case KF_YDOWN:
  156.             show_fixed (1);
  157.             CW->maxy -= WSTEP;
  158.             if (CW->maxy < FONE/128)
  159.                 CW->maxy = FONE/128;
  160.             show_fixed (0);
  161.             break;
  162.  
  163. #undef WSTEP        /* ++++++++++++++ end   debug keys ++++++++++++++ */
  164.  
  165.         case KF_ZOOMIN:
  166.             i = 1;
  167.             goto main_zoom;
  168.         case KF_ZOOMOUT:
  169.             i = -1;
  170.         main_zoom:
  171.             if ((!(st.flags1 & SF_EXTVIEW) || scenery (st.extview))
  172.                             && T(vp = CP))
  173.                 goto zoomit;
  174.             break;
  175.  
  176.         case KF_VZOOMIN:
  177.             i = 1;
  178.             goto ext_zoom;
  179.         case KF_VZOOMOUT:
  180.             i = -1;
  181.         ext_zoom:
  182.             if (st.flags1 & SF_EXTVIEW)
  183.                 goto main_zoom;
  184.             if (scenery (st.extview)
  185.                     && T(p = get_viewer (st.extview))
  186.                     && T(vp = p->viewport))
  187.                 goto zoomit;
  188.             break;
  189.  
  190.         zoomit:
  191.             zoom (vp, i);
  192.             update_viewer ();
  193.             break;
  194.  
  195.         case KF_MENU:
  196.             ret = menu_top ();
  197.             break;
  198.  
  199.         case 'B':
  200.             menu_btn ();
  201.             break;
  202.         case 'c':
  203.             msg_clear (0);
  204.             set_lists (0);
  205.             break;
  206.         case 'C':
  207.             if (ecv)
  208.                 SetOption (&ecv->flags, PF_CHASE);
  209.             break;
  210.         case 'd':
  211.             if (ecv)
  212.                 SetOption (&ecv->hudmode, HM_DECLUTTER);
  213.             break;
  214.         case 'D':
  215.             if (O_CHUTE == CV->name) {
  216.                 i = 5*2*VONE;        /* 2 seconds worth */
  217.                 if (CV->R[Z] > i)
  218.                     CV->R[Z] = i;
  219.                 if (CV->V[Z] > 0)
  220.                     CV->V[Z] = 0;
  221.             }
  222.             break;
  223.         case 'E':
  224.             if (CC->gpflags & GPF_PILOT)
  225.                 eject (CC);
  226.             break;
  227.         case 'f':
  228.             if (ecv) {
  229.                 if (ecv->radar & R_SELECT3)
  230.                     ecv->radar ^= R_SELECT3|R_SELECT20;
  231.                 else if (ecv->radar & R_SELECT20)
  232.                     ecv->radar ^= R_SELECT20|R_SELECT5;
  233.                 else if (ecv->radar & R_SELECT5)
  234.                     ecv->radar ^= R_SELECT5;
  235.                 else
  236.                     ecv->radar ^= R_SELECT3;
  237.                 ecv->target = 0;    /* re-select target */
  238.             }
  239.             break;
  240.         case 'h':
  241.         case '?':
  242.             set_lists (SF_HELP);
  243.             break;
  244.         case 'i':
  245.             if (ecv)
  246.                 SetOption (&ecv->radar, R_INTEL);
  247.             break;
  248.         case 'j':
  249.             if (ecv)
  250.                 SetOption (&ecv->radar, R_INTELCC);
  251.             break;
  252.         case 'k':
  253.             if (ecv)
  254.                 SetOption (&ecv->flags, PF_KILL);
  255.             break;
  256.         case 'l':
  257.             if (ecv)
  258.                 SetOption (&ecv->radar, R_LOCK);
  259.             break;
  260.         case 'm':
  261.             set_lists (SF_MODES);
  262.             break;
  263.         case 'M':
  264.             temp = st.btnmode;
  265.             SetOption (0, 2);
  266.             for (ch = 0; ch >= 0;) {
  267.                 ch = mgetch ();
  268.                 switch (ch) {
  269.                 case '0':
  270.                 case '1':
  271.                 case '2':
  272.                     SetOption (0, ch - '0');
  273.                     break;
  274.                 case 'a':
  275.                     SetOption (&temp, K_ALT);
  276.                     break;
  277.                 case 'c':
  278.                     SetOption (&temp, K_CTRL);
  279.                     break;
  280.                 case 's':
  281.                     SetOption (&temp, K_SHIFT);
  282.                     break;
  283.                 case 'p':
  284.                     SetOption (&temp, K_SPECIAL);
  285.                     break;
  286.                 case 'x':
  287.                     temp = 0;
  288.                     SetOption (0, 1);
  289.                     break;
  290.                 case '*':
  291.                     ch = -1;    /* cancel */
  292.                     break;
  293.                 default:
  294.                     st.btnmode = temp;
  295.                     ch = -1;    /* quit */
  296.                     break;
  297.                 }
  298.             }
  299.             break;
  300.         case 'n':
  301.             set_lists (SF_NET);
  302.             break;
  303.         case 'o':
  304.         case 'O':
  305.             if (st.network & NET_ON) {
  306.                 MsgWPrintf (50, "Net active!");
  307.                 break;
  308.             }
  309. #if 0
  310.             flags = st.flags;
  311.             st.flags |= SF_PAUSED;
  312. #endif
  313.             cv = 0;
  314.             for (i = 1, p = CO; p; p = p->next, ++i) {
  315.                 if (ch == 'o' && (p->name == O_M61 ||
  316.                           p->name == O_MK82 ||
  317.                           p->name == O_BROKEN))
  318.                     continue;
  319.                 if (p == CV) {
  320.                     j = 'V';
  321.                     cv = i;
  322.                 } else if (p->flags & F_CC)
  323.                     j = 'C';
  324.                 else if (IS_PLANE(CC) && p == EE(CC)->target)
  325.                     j = 'L';
  326.                 else
  327.                     j = ' ';
  328.                 sprintf (msg, "%c%3u ", j, i);
  329.                 sprintf (m, "%-6s", TITLE(p));
  330.                 strcat (msg, m);
  331.                 if (p->flags & F_IMPORTED) {
  332.                     if (p->rplayer)
  333.                         sprintf (m, " %s",
  334.                             p->rplayer->name);
  335.                     else
  336.                         sprintf (m, " I");
  337.                 } else if (p->flags & F_EXPORTED)
  338.                     sprintf (m, " ***");
  339.                 else
  340.                     m[0] = '\0';
  341.                 strcat (msg, m);
  342.                 sprintf (m, " (%ld,%ld,%ld)",
  343.                     vuscale (p->R[X]),
  344.                     vuscale (p->R[Y]),
  345.                     vuscale (p->R[Z]));
  346.                 strcat (msg, m);
  347.                 MsgPrintf (200, msg);
  348.             }
  349.             do {
  350.                 if (cv)
  351.                     sprintf (msg, "%d", cv);
  352.                 else
  353.                     msg[0] = '\0';
  354.                 edit_str ("choose viewer", msg, sizeof (msg));
  355.                 if ('\0' == msg[0]) {
  356.                     j = 0;
  357.                     break;
  358.                 }
  359.                 if ('c' == msg[0]) {
  360.                     j = -1;
  361.                     break;
  362.                 } else if ('l' == msg[0]) {
  363.                     j = -2;
  364.                     break;
  365.                 }
  366.             } while (1 != sscanf (msg, "%u", &j) ||
  367.                     j < 0 || j >= i);
  368.             if (0 == j)
  369.                 p = 0;
  370.             else if (-1 == j)
  371.                 p = CC;
  372.             else if (-2 == j) {
  373.                 if (IS_PLANE(CC))
  374.                     p = EE(CC)->target;
  375.             } else {
  376.                 for (i = 1, p = CO; p && i != j; ++i)
  377.                     p = p->next;
  378.             }
  379.             if (p) {
  380. #if 0
  381.                 if (!IS_PLANE(p))
  382.                     goto again;
  383. #endif
  384.                 save_viewport (CV);
  385.                 get_viewport (p);
  386.                 CV = p;
  387.             }
  388. #if 0
  389.             st.flags = flags;
  390. #endif
  391.             break;
  392.         case 'p':
  393.             temp = st.flags;
  394.             SetOption (&temp, SF_PAUSED);
  395.             pause_set (temp);
  396.             break;
  397.         case 'P':
  398.             i = mgetch ();
  399.             i = opt36 (i);
  400.             st.flags &= ~SF_INTERACTIVE;
  401.             if (CC->pointer && i >= 0)
  402.                 do_btn (CC->pointer, i, 1);
  403.             break;
  404.         case 'q':
  405.             st.quiet = (st.quiet+1)%3;
  406.             if (2 == st.quiet) {
  407.                 Snd->Effect (EFF_ENGINE, SND_ON);
  408.                 MsgPrintf (20, "Sound on");
  409.             } else {
  410.                 Snd->Effect (EFF_ENGINE, SND_OFF);
  411.                 if (0 == st.quiet) {
  412.                     Snd->List (NULL, SND_OFF);
  413.                     st.sounds = 0;
  414.                     MsgPrintf (20, "Sound off");
  415.                 } else
  416.                     MsgPrintf (20, "Sound on, no engine");
  417.             }
  418.             break;
  419.         case 'r':
  420.             if (ecv)
  421.                 SetOption (&ecv->radar, R_ON);
  422.             break;
  423.         case 'R':
  424.             i = mgetch ();
  425.             i = opt36 (i);
  426.             st.flags &= ~SF_INTERACTIVE;
  427.             if (CC->pointer && i >= 0)
  428.                 do_btn (CC->pointer, i, 0);
  429.             break;
  430.         case 's':
  431.             set_lists (SF_STATS);
  432.             break;
  433.         case 'S':
  434.             if (ecv)
  435.                 supply (CV, 1);
  436.             break;
  437.         case 'u':
  438.             if (ecv)
  439.                 menu_hud ();
  440.             break;
  441.         case 'v':
  442.             temp = st.flags1;
  443.             SetOption (&temp, SF_EXTVIEW);
  444.             if (!((temp ^ st.flags1) & SF_EXTVIEW))
  445.                 break;
  446.             if (scenery (st.extview)) {
  447.                 if (F(p = get_viewer (st.extview))) {
  448.                     MsgWPrintf (50, "No viewer");
  449.                     break;
  450.                 }
  451.             } else
  452.                 p = 0;
  453.             if (st.flags1 & SF_EXTVIEW) {
  454.                 if (p) {
  455.                     save_viewport (p);
  456.                     get_viewport (CV);
  457.                 }
  458.                 swap_view (HDT_FRONT, st.extview);
  459.             } else {
  460.                 if (p) {
  461.                     save_viewport (CV);
  462.                     get_viewport (p);
  463.                 }
  464.                 swap_view (st.extview, HDT_FRONT);
  465.             }
  466.             st.flags1 ^= SF_EXTVIEW;
  467.             break;
  468.         case 'w':
  469.             if (ecv)
  470.                 ecv->weapon = (ecv->weapon+1)%3;
  471.             break;
  472.         case 'W':
  473.             if (ecv)
  474.                 memset (ecv->stores, 0, sizeof (ecv->stores));
  475.             break;
  476.         case 'x':
  477.             (*CC->pointer->control->Cal)(CC->pointer);
  478.             break;
  479.         case '-':
  480.             CP->rotz += D90/2;
  481.             break;
  482.         case '*':
  483.             CP->rotx = 0;
  484.             CP->rotz = 0;
  485.             CP->rotz = 0;
  486.             break;
  487.         case '/':
  488.             CP->rotz -= D90/2;
  489.             break;
  490.         case '!':
  491.             Gr->Term (CS->device);
  492.             Sys->Shell ();
  493.             if (Gr->Init (CS->device, st.grname)) {
  494.                 LogPrintf ("device init failed after shell\n");
  495.                 die ();
  496.             }
  497.             set_palette ();
  498.             show_fixed (0);
  499.             break;
  500.         default:
  501.             (*CC->pointer->control->Key)(CC->pointer, ch);
  502.             break;
  503.     }
  504.     return (ret);
  505. }
  506.  
  507. LOCAL_FUNC void NEAR
  508. misc_actions (void)
  509. {
  510.     int    n, r;
  511.     OBJECT    *p;
  512.     HMSG    *m;
  513.  
  514.     if (st.drones) {            /* top up planes */
  515.         for (n = r = 0, p = CO; p; p = p->next) {
  516.             if (p->name == O_PLANE && !(p->flags & F_CC) &&
  517.                 !(p->flags & F_IMPORTED)) {
  518.                 ++n;
  519.                 if (EX->radar & R_ON)
  520.                     ++r;
  521.             }
  522.         }
  523.         if (n < st.drones && st.DroneTime <= st.present) {
  524.             emit_drone ();
  525.             st.DroneTime = st.present + TO_DRONE;
  526.         }
  527.         if (r < st.killers) {
  528.             for (p = CO; p; p = p->next) {
  529.                 if (p->name == O_PLANE &&
  530.                     !(p->flags & F_CC) &&
  531.                     !(p->flags & F_IMPORTED) &&
  532.                     !(EX->radar & R_ON)) {
  533.                     EX->radar |= R_ON|R_LOCK|R_INTELCC;
  534.                     EX->flags |= PF_CHASE | PF_KILL;
  535.                     EX->weapon = WE_M61;  /*fix 1*/
  536.                     if (++r >= st.killers)
  537.                         break;
  538.                 }
  539.             }
  540.         }
  541.     }
  542.  
  543.     if (st.ShutdownTime && st.present > st.ShutdownTime) {
  544.         static Ushort    keys[] = {KF_MENU, 'x', 'y'};
  545.  
  546.         mac_interpret (keys, rangeof (keys));
  547.     }
  548.  
  549.     if ((st.flags1 & SF_TESTING) && !st.ntargets) {    /* score result */
  550.         if (st.quiet)
  551.             Snd->Effect (EFF_GONE, SND_ON);
  552.  
  553.         MsgPrintf (100, "All gone");
  554.         m = MsgWPrintf (0, "Hit Any key");
  555.  
  556.         Kbd->Wait ();
  557.  
  558.         msg_del (m);
  559.  
  560.         if (st.quiet)
  561.             Snd->Effect (EFF_GONE, SND_OFF);
  562.         st.flags1 &= ~(SF_TESTING|SF_INFO);
  563.     }
  564.  
  565.     mem_check ();            /* top up memory */
  566.  
  567.     if (st.network & NET_AUTOCONNECT) {
  568.         if ((Ulong)STATS_NETAUTOTIME <= st.present) {
  569.             STATS_NETAUTOTIME = st.present + TO_AUTOCONNECT;
  570.             remote_ping ();
  571.         }
  572.     }
  573. }
  574.  
  575. extern int FAR
  576. user_command (void)
  577. {
  578.     int    i, ch;
  579.  
  580.     do {
  581.         sys_poll (0);
  582.         misc_actions ();
  583.         for (i = 0; -1 != (ch = mread ());) {
  584.             SetOption (0, 2);
  585.             st.flags |= SF_INTERACTIVE;
  586.             i = one_command (ch);
  587.             st.flags &= ~SF_INTERACTIVE;
  588.             if (i)
  589.                 break;
  590.             if (T(i = st.flags1 & SF_TERM))
  591.                 break;
  592.         }
  593.     } while (!i);
  594.     return (i);
  595. }
  596.  
  597.  
  598. /* screen menu
  599. */
  600.  
  601. static MENU FAR MenuCmd[] = {
  602.     {'0', "off"},        /*  0 */
  603.     {'1', "on"},        /*  1 */
  604.     {'2', "toggle"},    /*  2 */
  605. {'\0', 0}};
  606.  
  607. extern int FAR
  608. menu_cmd (void)
  609. {
  610.     int    sel, ret;
  611.     HMSG    *m;
  612.  
  613.     ret = 0;
  614.     SetOption (0, 2);
  615.     sel = menu_open (MenuCmd, 2);
  616.     switch (sel) {
  617.     case MENU_ABORTED:
  618.     default:
  619.         break;
  620.     case 0:
  621.     case 1:
  622.     case 2:
  623.         SetOption (0, sel);
  624.         m = MsgWPrintf (0, "Enter any command");
  625.         ret = one_command (mgetch ());
  626.         msg_del (m);
  627.         break;
  628.     }
  629.     if (MENU_FAILED != sel)
  630.         menu_close ();
  631.  
  632.     return (ret);
  633. }
  634.